package com.yfp.client.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.hutool.json.JSONUtil;
import com.yfp.client.annotation.AnonymousAccess;
import com.yfp.client.config.WxMaConfiguration;
import com.yfp.client.config.WxPayProperties;
import com.yfp.client.service.WxInfoService;
import com.yfp.client.util.Response;
import com.yfp.common.core.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/client/wx-info")
public class WxInfoController extends BaseController {

    @Resource
    private WxPayProperties wxPayProperties;
    @Resource
    private WxInfoService wxInfoService;

    @GetMapping("/validate")
    @AnonymousAccess
    @Operation(summary = "发起验证", description = "消息推送-发起验证")
    public String getValidate(@RequestParam(value = "signature", required = false) String signature, @RequestParam(value = "timestamp", required = false) String timestamp, @RequestParam(value = "nonce", required = false) String nonce, @RequestParam(value = "echostr", required = false) String echostr) {
        WxMaService wxService = WxMaConfiguration.getMaService(wxPayProperties.getConfigs().get(0).getAppId());
        boolean validate = wxService.checkSignature(timestamp, nonce, signature);
        if (validate) {
            return echostr;
        }
        return "";
    }

    @PostMapping("/validate")
    @AnonymousAccess
    @Operation(summary = "接收消息推送", description = "消息推送-接收消息推送")
    public String postValidate(@RequestParam(value = "signature", required = false) String signature, @RequestParam(value = "timestamp", required = false) String timestamp, @RequestParam(value = "nonce", required = false) String nonce, @RequestParam(value = "echostr", required = false) String echostr, @RequestBody Object object) {
        String jsonStr = JSONUtil.toJsonStr(object);
        WxMaMessage wxMaMessage = WxMaMessage.fromJson(jsonStr);
        log.info("postValidate,wxMaMessage: {}", wxMaMessage);
        return "";
    }

    @AnonymousAccess
    @GetMapping("/template-list")
    @Operation(summary = "获取模板列表", description = "订阅消息-获取模板列表")
    public Response<List<TemplateInfo>> getTemplateList() {
        WxMaService wxService = WxMaConfiguration.getMaService(wxPayProperties.getConfigs().get(0).getAppId());
        WxMaSubscribeService subscribeService = wxService.getSubscribeService();
        try {
            List<TemplateInfo> templateList = subscribeService.getTemplateList();
            log.info("templateInfo: {}", JSONUtil.toJsonStr(templateList));
            return Response.success(templateList);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @AnonymousAccess
    @GetMapping("/notify/cooking-status")
    @Operation(summary = "发送炒制状态通知")
    public Response<Void> notifyCookingStatus(@RequestParam("orderId") String orderId, @RequestParam("orderDetailId") String orderDetailId) {
        wxInfoService.notifyCookingStatus(orderId, orderDetailId);
        return Response.success();
    }

    @AnonymousAccess
    @GetMapping("/notify/take-food")
    @Operation(summary = "发送取餐提醒通知")
    public Response<Void> notifyTakeFood(@RequestParam("orderId") String orderId, @RequestParam("orderDetailId") String orderDetailId, @RequestParam("date") Date date) {
        wxInfoService.notifyTakeFood(orderId, orderDetailId, date);
        return Response.success();
    }
}